<template>
  <div class="serach-result">
    <!-- 导航栏   :title="`${this.$route.query.keyword}的搜索结果`"-->
    <van-nav-bar
      :title="$route.query.keyword + '的搜索结果'"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->

    <!-- 文章列表 -->
    <van-list
      class="article-list"
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ArtListTemplate :artList="list" :isView="true" />
    </van-list>
    <!-- /文章列表 -->
  </div>
</template>

<script>
import { getSearch } from "@/api/search.js";
import ArtListTemplate from "@/components/ArtListTemplate";
export default {
  name: "SearchResult",
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1
    };
  },
  components: {
    ArtListTemplate
  },
  created() {
    //console.log(this.$router,this.$route,'index页面带过来的内容啊')
  },
  methods: {
    async onLoad() {
      /**
       * {
        keyword:this.$route.query.keyword,
        this.page
      }
       */
      const res = await getSearch({
        keyword: this.$route.query.keyword,
        page: this.page
      });
      const results = res.data.data.results;
      // [...[1,2,3,4]] => [1,2,3,4]
      // 把我们的数据添加到 list
      this.list.push(...results);
      //
      this.loading = false;
      // 翻页
      this.page++;
      if (results.length === 0) {
        this.finished = true;
      }
    }
  }
};
</script>

<style lang="less" scoped>
.serach-result {
  height: 100%;
  position: absolute;
  overflow: auto;
  .article-list {
    margin-top: 50px;
  }
}
</style>
